package com.too.learn.drools.util;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

/**
 * OOS类型的数据的解析工具类
 *
 * @author ouyf
 * @date 2024/11/15 11:12
 * <p>
 * Copyright (c) Siemens Mobility TBDS 2024, All Rights Reserved, Confidential
 */
public class OosParseUtil {

    /**
     * 整体针对OOS中的属性进行解析
     *
     * @param oos :
     * @return : void
     * @author : ouyf
     * @date : 2024/11/15
     */
    public static void oosTransform(Map<String, Object> oos) {
        if (MapUtils.isNotEmpty(oos)) {
            String key = "TrainDoorStatus";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "所有门关");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "左门开，右门关");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "左门关，右门开");
            } else if (MapUtils.getIntValue(oos, key) == 3) {
                oos.put(key, "左门开，右门开");
            } else {
                oos.put(key, "");
            }

            key = "EBIsApplied";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "紧制未实施");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "已实施紧制");
            } else {
                oos.put(key, "");
            }

            key = "MaxLevel";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "未定义");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "IXLC");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "ITC");
            } else if (MapUtils.getIntValue(oos, key) == 3) {
                oos.put(key, "CTC");
            } else {
                oos.put(key, "");
            }

            key = "MaxMode";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "未定义");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "RM");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "SM");
            } else if (MapUtils.getIntValue(oos, key) == 3) {
                oos.put(key, "AM");
            } else if (MapUtils.getIntValue(oos, key) == 6) {
                oos.put(key, "XAM");
            } else {
                oos.put(key, "");
            }

            key = "ActiveCab";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "未定义");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "远端");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "近端");
            } else {
                oos.put(key, "");
            }

            key = "HoldStationExit";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "否");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "是");
            } else {
                oos.put(key, "");
            }

            key = "HoldAtPlatform";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "否");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "是");
            } else {
                oos.put(key, "");
            }

            key = "PTNotAccessible";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "否");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "是");
            } else {
                oos.put(key, "");
            }

            key = "StationDepartureStatus";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "否");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "是");
            } else {
                oos.put(key, "");
            }

            key = "CurrentReversalStrategy";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "无折返");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "无人折返");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "换端");
            } else {
                oos.put(key, "");
            }

            key = "NextOSP";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "未定义");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "前");
            } else if (MapUtils.getIntValue(oos, key) == 2) {
                oos.put(key, "中");
            } else if (MapUtils.getIntValue(oos, key) == 3) {
                oos.put(key, "后");
            } else {
                oos.put(key, "");
            }

            key = "OPRValidFlag";
            if (MapUtils.getIntValue(oos, key) == 0) {
                oos.put(key, "否");
            } else if (MapUtils.getIntValue(oos, key) == 1) {
                oos.put(key, "是");
            } else {
                oos.put(key, "");
            }

            key = "OBCUState";
            Integer value = MapUtils.getIntValue(oos, key);
            oos.put(key, obcuStateParse(value));

            CustoParseUtil.transMapWithCustoDict(oos);
            CustoParseUtil.transPtId2Name(oos);
            CustoParseUtil.transObcuId2Name(oos);
            CustoParseUtil.transSegId(oos);
        }
    }

    /**
     * 根据obcu状态二进制字符串解析obcu状态
     * 传入为固定的两位二进制码：4-5位为A1状态，6-7位为A2状态
     *
     * @param binaryStr :
     * @return : java.lang.String
     * @author : ouyf
     * @date : 2024/11/14
     */
    public static String obcuStateDict(String binaryStr) {
        String name = "";
        switch (binaryStr) {
            case "00":
                name = "不可用";
                break;
            case "01":
                name = "备用";
                break;
            case "10":
                name = "主用";
                break;
            case "11":
                name = "自动复位失败";
                break;
        }
        return name;
    }

    public static String obcuStateParse(Integer obcuState) {
        if (obcuState != null) {
            StringBuilder sb = new StringBuilder();
            String obcuStateStr = StringUtils.leftPad(Integer.toBinaryString(96), 8, "0");
            sb.append("A1冗余状态=").append(obcuStateDict(obcuStateStr.substring(2, 4))).append(",");
            sb.append("A2冗余状态=").append(obcuStateDict(obcuStateStr.substring(0, 2)));
            return sb.toString();
        }
        return null;
    }

    /**
     * 针对OOS中opr属性进行解析
     *
     * @param opr :
     * @return : void
     * @author : ouyf
     * @date : 2024/11/15
     */
    public static void oprTransform(Map<String, Object> opr) {
        if (MapUtils.isNotEmpty(opr)) {
            String key = "LocIsSecured";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "否");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "是");
            } else {
                opr.put(key, "");
            }

            key = "LocIntegrityIsOk";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "否");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "是");
            } else {
                opr.put(key, "");
            }

            key = "FrontTravelDir";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "+");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "-");
            } else {
                opr.put(key, "");
            }

            key = "RearTravelDir";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "+");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "-");
            } else {
                opr.put(key, "");
            }

            key = "CurrentOSP";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "前");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "中");
            } else if (MapUtils.getIntValue(opr, key) == 3) {
                opr.put(key, "后");
            } else {
                opr.put(key, "");
            }

            key = "OperationLevel";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "IXLC");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "ITC");
            } else if (MapUtils.getIntValue(opr, key) == 3) {
                opr.put(key, "CTC");
            } else {
                opr.put(key, "");
            }

            key = "OperationMode";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "RM");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "SM");
            } else if (MapUtils.getIntValue(opr, key) == 3) {
                opr.put(key, "AM");
            } else if (MapUtils.getIntValue(opr, key) == 6) {
                opr.put(key, "XAM");
            } else {
                opr.put(key, "");
            }

            key = "TrainStandStill";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "否");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "是");
            } else {
                opr.put(key, "");
            }

            key = "PSDOpenAuth";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "无授权");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "已授权");
            } else {
                opr.put(key, "");
            }

            key = "StopAssure";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "否");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "是");
            } else {
                opr.put(key, "");
            }

            key = "VitalMALDirection";
            if (MapUtils.getIntValue(opr, key) == 0) {
                opr.put(key, "未定义");
            } else if (MapUtils.getIntValue(opr, key) == 1) {
                opr.put(key, "+");
            } else if (MapUtils.getIntValue(opr, key) == 2) {
                opr.put(key, "-");
            } else {
                opr.put(key, "未定义");
            }

            key = "TrainSpeed";
            Integer valueB = MapUtils.getIntValue(opr, key);
            BigDecimal speed = new BigDecimal(valueB);
            if (valueB != null) {
                speed = speed.multiply(new BigDecimal(0.036)).setScale(0, BigDecimal.ROUND_DOWN);
                opr.put("TrainSpeed", speed.toString());
            }

            CustoParseUtil.transMapWithCustoDict(opr);
            CustoParseUtil.transPtId2Name(opr);
            CustoParseUtil.transSegId(opr);
        }
    }

    /**
     * 整体对OOS中missionCode属性进行解析
     * 基于10进制的serviceNum和serialNumLow, serialNumHigh生成车次号tripNumber
     * 算法：3位左填0的serviceNum + 3位左填0的十进制(16进制的serialNumHigh + 16进制的serialNumLow)
     *
     * @param missionCodeMap,包括serviceNum、serialNumLow、serialNumHigh
     * @return : java.lang.String
     * @author : ouyf
     * @date : 2024/11/6
     */
    public static String genTripNumber(Map<String, String> missionCodeMap) {
        if (MapUtils.isNotEmpty(missionCodeMap)) {
            Integer serviceNum = MapUtils.getIntValue(missionCodeMap, "ServiceNum");
            Integer serialNumLow = MapUtils.getIntValue(missionCodeMap, "serialNumLow");
            Integer serialNumHigh = MapUtils.getIntValue(missionCodeMap, "serialNumHigh");
            BigInteger serialNum = new BigInteger(String.format("%02x%02x", serialNumHigh, serialNumLow), 16);
            return String.format("%03d%03d", serviceNum, serialNum);
        } else {
            return null;
        }
    }
}
